package com.yundingai.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author zhaoj
 * @Date 2025/7/18 15:16
 * @Description RabbitMQ配置类，启动创建topic交换机并创建chat和sdp队列与其绑定
 **/
@Configuration
public class RabbitConfig {
    //定义交换机、队列名称
    public static final String ROOM_EXCHANGE_NAME = "room_exchange";
    public static final String ED_EXCHANGE_NAME = "excalidraw_exchange";
    public static final String SDP_QUEUE_PREFIX = "sdp.";
    public static final String CHAT_QUEUE_PREFIX = "chat.";
    public static final String EXCALIDRAW_QUEUE_PREFIX = "draw.";
    //声明Topic交换机
    @Bean
    public TopicExchange topicExchange(){
        return ExchangeBuilder.topicExchange(ROOM_EXCHANGE_NAME).durable(false).build();
    }
    //声明excalidraw交换机
    @Bean
    public TopicExchange excalidrawExchange(){
        return ExchangeBuilder.topicExchange(ED_EXCHANGE_NAME).durable(false).build();
    }
    //声明默认监听队列
    @Bean
    public Queue sysSdpQueue(){
        return QueueBuilder.nonDurable("sdp_queue").autoDelete().build();
    }
    @Bean
    public Queue sysChatQueue(){
        return QueueBuilder.nonDurable("chat_queue").autoDelete().build();
    }
    @Bean
    public Queue sysExcalidrawQueue(){
        return QueueBuilder.nonDurable("excalidraw_queue").autoDelete().build();
    }
    @Bean
    public Binding bindSdpQueue(){
        return BindingBuilder.bind(sysSdpQueue()).to(topicExchange()).with(SDP_QUEUE_PREFIX+"#");
    }
    @Bean
    public Binding bindChatQueue(){
        return BindingBuilder.bind(sysChatQueue()).to(topicExchange()).with(CHAT_QUEUE_PREFIX+"#");
    }
    @Bean
    public Binding bindExcalidrawQueue(){
        return BindingBuilder.bind(sysExcalidrawQueue()).to(excalidrawExchange()).with(EXCALIDRAW_QUEUE_PREFIX+"#");
    }
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
